#include <main.h>
#include <2416.C>
#include <ds1307.c>
#int_EXT
int16 gValtimer1=0,gTest=0;
int16 order[3]={0},cmd[4]={0};
BYTE gCount,gYear=0,gMth=0,gDay=0,gDow=0,gHou=0,gMin=0,gSec=0;
void  EXT_isr(void) 
{
   
   
   /*
   output_high(PIN_E1);
   output_d(0x55);
   delay_ms(100);
   output_low(PIN_E1);*/
}

#int_TIMER1
void  TIMER1_isr(void) 
{
   /*output_high(PIN_A0);
   output_d(0x55);
   delay_ms(100);
   output_low(PIN_A0);*/
}

#int_TIMER2
void  TIMER2_isr(void) 
{
   /*output_high(PIN_E2);
   output_d(0xaa);
   delay_ms(100);
   output_low(PIN_E2);*/
}

#int_CCP1
void  CCP1_isr(void) 
{
   int16 secDelay=0, minDelay=0,houDelay=0;
   int16 arrDelay[15]={0};
   int8 i=0;
    
   gValtimer1 = (get_timer1()/75);
   set_timer1(0);
   rtc_get_time(&gHou,&gMin,&gSec);
   for(i=0;i<12;i++)
         arrDelay[i]= gValtimer1*5;
   computeDelayTime(&secDelay, &minDelay,&houDelay,gHou,gMin,gSec);
   arrDelay[12]= secDelay;
   arrDelay[13]= minDelay;
   arrDelay[14]= houDelay;
   sort(arrDelay,15);
   for(i=0;i<15;i++)
   {
         output_high(PIN_A0);
         put_byte(0x01);
         if(arrDelay[i]== secDelay ||arrDelay[i]== minDelay ||arrDelay[i]== houDelay )
            delay_us(arrDelay[i]- gValtimer1*5*i);
         else
            delay_us(arrDelay[i]);
         output_low(PIN_A0);
         if(arrDelay[i]==secDelay){
            create_second_hand(cmd);
            display_frame(cmd);
         }
         else if(arrDelay[i]==minDelay){
            create_minute_hand(cmd);
            display_frame(cmd);
         }
         else if(arrDelay[i]==houDelay){   
            create_hour_hand(cmd);
            display_frame(cmd);
         }
         else{     
            output_high(PIN_A0);
            put_byte(0xD);
            output_low(PIN_A0);
            
         }            
      }     
}



void main()
{
   int16 secDelay=0, minDelay=0,houDelay=0;
   int16 arrDelay[15]={0};
   int8 i=0;
   port_b_pullups(TRUE);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);      //52.4 ms overflow
   setup_timer_2(T2_DIV_BY_4,0,1);      //.8 us overflow, .8 us interrupt
   setup_ccp1(CCP_CAPTURE_FE);
   setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
   init_ext_eeprom();
   rtc_init(0,1,0);
  // enable_interrupts(INT_EXT);
   //enable_interrupts(INT_TIMER1);
  // enable_interrupts(INT_TIMER2);
   enable_interrupts(INT_CCP1);
   enable_interrupts(GLOBAL);
   while(1);
   while(1){
  // gValtimer1=gTest;
     
   
   for(i=0;i<12;i++)
         arrDelay[i]= gValtimer1*5;
   computeDelayTime(&secDelay, &minDelay,&houDelay,gHou,gMin,gSec);
   arrDelay[12]= secDelay;
   arrDelay[13]= minDelay;
   arrDelay[14]= houDelay;
   sort(arrDelay,15);
   for(i=0;i<15;i++)
   {
         output_high(PIN_A0);
         put_byte(0x01);
         delay_us(arrDelay[i]);
         output_low(PIN_A0);
         if(arrDelay[i]==secDelay){
            create_second_hand(cmd);
            display_frame(cmd);
         }
         else if(arrDelay[i]==minDelay){
            create_minute_hand(cmd);
            display_frame(cmd);
         }
         else if(arrDelay[i]==houDelay){   
            create_hour_hand(cmd);
            display_frame(cmd);
         }
         else{     
            output_high(PIN_A0);
            put_byte(0xD);
            delay_us(100);
            output_low(PIN_A0);
            
         }
            
      }
      
        
      }
  
   }

void boundary(){
   int8 i=0;
    for(i=0;i<12;i++){
     output_high(PIN_A0);
     put_byte(0x01);
     delay_us(gValtimer1*5);
     put_byte(0xD);
     put_byte(0x01);
     }
     i=get_timer1()*0.8;
     
     delay_us(gValtimer1*60 - i );
     
     output_low(PIN_A0);
}
void computeDelayTime(int16 *secDelay,int16 *minDelay,int16 *houDelay,BYTE gHou,BYTE gMin,BYTE gSec ){
      *secDelay=(int16) gSec*gValtimer1; 
      *minDelay=(int16) gMin*gValtimer1; 
      *houDelay=(int16) (5*gHou*gValtimer1 + (gMin*gValtimer1)/10);   
}


void create_second_hand(BYTE cmd[]){
   cmd[0]=0x00;
   cmd[1]=0xaa;
   cmd[2]=0xff;
   cmd[3]=0xff;
}
void create_minute_hand(BYTE cmd[]){
   cmd[0]=0x00;
   cmd[1]=0x00;
   cmd[2]=0xaa;
   cmd[3]=0xaa;
}
void create_hour_hand(BYTE cmd[]){
   cmd[0]=0x00;
   cmd[1]=0x00;
   cmd[2]=0x00;
   cmd[3]=0x55;
      
   
}

void display_frame(BYTE cmd[]){
 
     output_high(PIN_A0);
     put_byte(cmd[0]); 
     output_low(PIN_A0);
     
     output_high(PIN_E0);
     put_byte(cmd[1]);    
     output_low(PIN_E0);
     
     output_high(PIN_E1);
     put_byte(cmd[2]);    
     output_low(PIN_E1);
     
     output_high(PIN_E2);
     put_byte(cmd[3]);    
     output_low(PIN_E2);
     
         
}
void put_byte(BYTE cmd){
   output_d(cmd);
   delay_us(100);
}
void sort(int16 arr[],int8 length)
{
   int16 i=0,j=0,swap=0;
   for(i=0;i<length;i++) {
      for( j=length-1;j>i;j--) {
         if(arr[j]<arr[i]) {
            swap = arr[i];
            arr[i] = arr[j];
            arr[j] = swap;
         }
      }
   }
}
